﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnlineStore.Dal
{
    public class DalUser
    {
        OnlineStoreDataContext data = new OnlineStoreDataContext();
        public DalUser()
        {
            
        }
        internal tbCustomer UpdateInfo(tbCustomer user, string FullName, string Address, string Tel)
        {
            try
            {
                tbCustomer User = data.tbCustomers.First(p => p.Cus_ID == user.Cus_ID);
                User.Cus_Name = FullName;
                User.Address = Address;
                User.Phone = Tel;
                data.SubmitChanges();
                return user;
            }
            catch{
                return null;
            }
        }
        internal tbCustomer getUser(string UserName, string PassWord)
        {
            PassWord = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PassWord, "SHA1");
            var user = (from p in data.tbCustomers
                        where p.Username == UserName && p.Password == PassWord
                        select p).FirstOrDefault();
            if (user == null) return null;
            return (tbCustomer)user;
        }
        internal tbCustomer addUser(string UserName, string Pass, string FullName, string Tel, string Address)
        {

            if (isUser(UserName)) return null;
            Pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Pass, "SHA1");
            tbCustomer user = new tbCustomer();
            user.Cus_ID = Max + 1;
            user.Username = UserName;
            user.Password = Pass;
            user.Phone = Tel;
            user.Cus_Name = FullName;
            user.Address = Address;
            data.tbCustomers.InsertOnSubmit(user);
            data.SubmitChanges();
            return user;
        }
        public int Max
        {
            get {
                var list = (from p in data.tbCustomers
                            select p.Cus_ID).ToList();
                if (list.Count == 0) return 0;
                return list.Max();
            }
        }
        internal tbCustomer User(string UserName)
        {
           tbCustomer user = (from p in data.tbCustomers
                         where p.Username == UserName
                         select p).FirstOrDefault();
            return user;
        }
        public static bool isUser(string UserName)
        {
            OnlineStoreDataContext data = new OnlineStoreDataContext();
            tbCustomer user = (from p in data.tbCustomers
                               where p.Username == UserName
                               select p).FirstOrDefault();
            return user != null;
        }

        internal tbCustomer UpdatePassWord(tbCustomer user,string PassWord, string PassWordNew)
        {
            try
            {
                if(user.Password!=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PassWord, "SHA1")) return null;
                PassWordNew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PassWordNew, "SHA1");
                tbCustomer _user = (from p in data.tbCustomers
                                    where p.Cus_ID == user.Cus_ID
                                    select p).FirstOrDefault();
                _user.Password = PassWordNew;
                data.SubmitChanges();
                return _user;
            }
            catch
            {
                return null;
            }
        }
    }
}